<template>
  <div ref="echartsRef" class="card content-box"></div>
</template>

<script setup lang="ts" name="pieChart">
import { ref } from "vue";
import * as echarts from "echarts";
import { useEcharts } from "@/hooks/useEcharts";

const echartsRef = ref<HTMLElement>();
const initChart = (data: any) => {
  let myChart: echarts.ECharts = echarts.init(echartsRef.value as HTMLElement);
  let option: echarts.EChartsOption = {
    tooltip: {
      trigger: "item",
      formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    legend: {
      left: "center",
      top: "bottom",
      data: [
        data.value.goodsName[0],
        data.value.goodsName[1],
        data.value.goodsName[2],
        data.value.goodsName[3],
        data.value.goodsName[4],
        data.value.goodsName[5],
        data.value.goodsName[6],
        data.value.goodsName[7]
      ],
      textStyle: {
        color: "#a1a1a1"
      }
    },
    toolbox: {
      show: true,
      feature: {
        mark: { show: true },
        dataView: { show: true, readOnly: false },
        restore: { show: true },
        saveAsImage: { show: true }
      }
    },
    series: [
      {
        name: "Radius Mode",
        type: "pie",
        radius: [60, 200],
        center: ["50%", "50%"],
        roseType: "radius",
        itemStyle: {
          borderRadius: 5
        },
        label: {
          show: true
        },
        emphasis: {
          label: {
            show: true
          }
        },
        data: [
          { value: data.value.goodsSale[0], name: data.value.goodsName[0] },
          { value: data.value.goodsSale[1], name: data.value.goodsName[1] },
          { value: data.value.goodsSale[2], name: data.value.goodsName[2] },
          { value: data.value.goodsSale[3], name: data.value.goodsName[3] },
          { value: data.value.goodsSale[4], name: data.value.goodsName[4] },
          { value: data.value.goodsSale[5], name: data.value.goodsName[5] },
          { value: data.value.goodsSale[6], name: data.value.goodsName[6] },
          { value: data.value.goodsSale[7], name: data.value.goodsName[7] }
        ]
      }
    ]
  };
  useEcharts(myChart, option);
};

defineExpose({
  initChart
});
</script>

<style scoped lang="scss">
@import "./index.scss";
</style>
